package com.mad.base.gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.util.ArrayList;

import com.mad.base.manager.GuiManager;
import com.mad.base.objects.GameObject;
import com.mad.base.system.GameSystem;
import com.mad.base.system.ID;
import com.mad.base.system.IUpdate;

public class MainWnd extends MADWnd implements IUpdate{
    
	private static final long serialVersionUID = 1L;

	private final GuiManager guiManager ; 
	private final GridLayout layout ;
	private final EWndType wndType ; 
	private MADPanel[][] fields ; 
	
	public GuiManager getGuiManager() {
		return guiManager;
	}

	public MainWnd(GuiManager guiManager, int gridWidth, int gridHeight, EWndType wndType){
		this.guiManager = guiManager ; 
		this.fields = new MADPanel[gridWidth][gridHeight]; 
        this.layout = new GridLayout(gridWidth, gridHeight); 
        this.wndType = wndType ;
		this.init(null); 
	}
	
	@Override
	public void init(Object o) {
		this.setLayout(this.layout); 
		for(int x=0; x<this.fields.length; x++)
			for(int y=0; y<this.fields[x].length; y++){
				this.fields[x][y] = new MADPanel(ID.getIDByCoords(x, y)); 
				this.add(this.fields[x][y]); 
			}
		this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
		this.setSize(SIZE); 
		Point location = GameSystem.centerToScreen(SIZE) ; 
		this.setLocation((int)location.getX()+this.wndType.getOffsetXPosition(), (int)location.getY()); 
		this.setTitle("MADSeaWar - "+this.wndType.getTitle()); 
		this.setVisible(true); 
	}

    @Override
    public void update( Object o )
    {
        if(o instanceof ArrayList){
            ArrayList<ID> list = (ArrayList)o ; 
            for(Object tmp: list){
                if(tmp instanceof ID){
                    ID id = (ID) tmp ; 
                    this.fields[ID.convertX( id.getX() )][ID.convertY( id.getY() )].getFieldCoverer().setCovered( true ); 
                }
            }
        }
    }

}
